package org.kazuhiko.rpg.core;

import java.util.ArrayList;
import java.util.HashMap;

import org.kazuhiko.rpg.objects.Player;
import org.lwjgl.util.vector.Vector3f;

public class World
{
	private ArrayList<Drawable> drawObjects;
	private ArrayList<Drawable> deleteObjects;
	private ArrayList<Integer> handles;
	
	private HashMap<String, String> globalChat;
	
	private float width;
	private float height;
	
	public World()
	{
		init();
	}
	
	private void init()
	{
		drawObjects = new ArrayList<Drawable>();
		deleteObjects = new ArrayList<Drawable>();
		handles = new ArrayList<Integer>();
		globalChat = new HashMap<String, String>();
		
		width = 400;
		height = 400;
	}
	
	public void addPlayer( int id, String username, Vector3f position, Vector3f rotation )
	{
		Player player = new Player( username, false );
		player.setId( id );
		player.setPosition( position );
		player.setRotation( rotation );
		
		drawObjects.add( player );
	}
	
	public Player getPlayer( String username )
	{
		Player ret = null;
		
		for( Drawable obj : drawObjects )
		{
			if( obj instanceof Player )
			{
				Player player = ( Player ) obj;
				if( player.getName().equals( username ) )
				{
					ret = player;
				}
			}
		}
		
		return ret;
	}
	
	public void tick()
	{
		for( Drawable deleteObject : deleteObjects )
		{
			drawObjects.remove( deleteObject );
		}
		
		deleteObjects.clear();
		
		for( Drawable drawObject : drawObjects )
		{
			drawObject.tick();
			
			if( drawObject.hasFlag( Flag.DELETE ) )
			{
				deleteObjects.add( drawObject );
			}
		}
	}
	
	public ArrayList<Drawable> getDrawObjects()
	{
		return drawObjects;
	}
	
	public ArrayList<Integer> getHandles()
	{
		return handles;
	}
	
	public HashMap<String, String> getGlobalChat()
	{
		return globalChat;
	}
	
	public float getWidth()
	{
		return width;
	}
	public float getHeight()
	{
		return height;
	}
}
